package com.proton.carepatchtemp.utils.Pdf;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import com.proton.carepatchtemp.utils.UiUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Hloou on 2016/6/14.
 * 绘制PDF内容页
 */
public class EcgDraw extends View {
    public int sumLines = 0;
    /**
     * 分别是X,Y方向；不同粗细的paint
     **/
    Paint Xpaint;
    Paint Ypaint;
    Paint XXpaint;
    Paint YYpaint;
    Paint BARpaint;
    Paint DataPaint;
    int spaceX = (int) UiUtils.Xmm2px((float) 5, getContext());
    int spaceY = (int) UiUtils.Ymm2px((float) 5, getContext());
    int pointX;
    int pointY;
    int width;
    int height;
    float len = UiUtils.Xmm2px((float) 0.2, getContext()); //多少距离画一个点
    List<Double> dealDatas = new ArrayList<>();
    private int page = 1;

    public EcgDraw(Context context) {
        super(context);
    }
    public EcgDraw(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        InitPaint();
        DrawBackgroud(canvas);
        if (page == 1) {
            DrawBar(canvas);
            DrawData(canvas);
        } else {
            //LogUtils.e("page=Other:"+dealDatas);
            DrawDataOtherPage(canvas);
        }

    }

    /***
     * 初始化Paint
     */
    public void InitPaint() {
        Xpaint = new Paint();
        XXpaint = new Paint();
        Ypaint = new Paint();
        YYpaint = new Paint();
        BARpaint = new Paint();
        DataPaint = new Paint();
        DataPaint.setColor(Color.BLACK);
        DataPaint.setStrokeJoin(Paint.Join.ROUND);
        DataPaint.setStrokeWidth(1);
        DataPaint.setAntiAlias(true);
        BARpaint.setColor(Color.BLACK);
        BARpaint.setStrokeJoin(Paint.Join.ROUND);
        BARpaint.setStrokeWidth(8);
        Xpaint.setColor(Color.GRAY);
        Xpaint.setStrokeJoin(Paint.Join.ROUND);
        Xpaint.setStrokeWidth(1);
        XXpaint.setColor(Color.BLACK);
        XXpaint.setStrokeJoin(Paint.Join.ROUND);
        XXpaint.setStrokeWidth(3);
        Ypaint.setColor(Color.GRAY);
        Ypaint.setStrokeJoin(Paint.Join.ROUND);
        Ypaint.setStrokeWidth(1);
        YYpaint.setColor(Color.BLACK);
        YYpaint.setStrokeJoin(Paint.Join.ROUND);
        YYpaint.setStrokeWidth(3);
    }

    /**
     * 绘制心率图的背景
     *
     * @param canvas 画布
     */
    public void DrawBackgroud(Canvas canvas) {
        canvas.drawColor(Color.TRANSPARENT);
        pointX = (int) UiUtils.Ymm2px((float) 1, getContext());   //X点间隔
        pointY = (int) UiUtils.Ymm2px((float) 1, getContext());   //Y点间隔
        width = spaceX * 5 * 8;  //hdpi 480x800
        height = spaceY * 6 * 8;
        int LastXline = 0;  //最后X线Y坐标
        int LastYline = 0;  //最后Y线X坐标
        int countX = 0;
        int countY = 0;
        // 绘制X线
        while (height >= LastXline) {
            if (countX % 6 == 0) {
                canvas.drawLine(0, LastXline, width, LastXline, XXpaint);
            } else {
                canvas.drawLine(0, LastXline, width, LastXline, Xpaint);
            }
            countX++;
            LastXline += spaceX;
        }
        sumLines = countX;
        //绘制Y线
        while (width >= LastYline) {
            if (countY % 5 == 0) {
                canvas.drawLine(LastYline, 0, LastYline, height, YYpaint);
            } else {
                canvas.drawLine(LastYline, 0, LastYline, height, Ypaint);
            }
            countY++;
            LastYline += spaceY;
        }

        for (int i = 0; i * pointX < width; i++) {
            for (int j = 0; j * pointY < height; j++) {
                canvas.drawPoint(pointX * i, pointY * j, Xpaint);
            }
        }

    }

    public void DrawBar(Canvas canvas) {
        //绘制Bar
        canvas.drawLine(spaceX / 2, spaceY * 6, spaceX, spaceY * 6, BARpaint);
        canvas.drawLine(spaceX, spaceY * 6, spaceX, spaceY * 4, BARpaint);
        canvas.drawLine(spaceX, spaceY * 4, spaceX * 2, spaceY * 4, BARpaint);
        canvas.drawLine(spaceX * 2, spaceY * 4, spaceX * 2, spaceY * 6, BARpaint);
        canvas.drawLine(spaceX * 2, spaceY * 6, spaceX / 2 * 5, spaceY * 6, BARpaint);
    }

    public void DrawData(Canvas canvas) {
        int y_SmallCell = 6;
        for (int i = 0; i < dealDatas.size() - 1; i++) {
            if ((int) Math.floor((i + 25 * 3) / 1000) == 0) {
                canvas.drawLine((3 * spaceX + i * len)
                        , UiUtils.Xmm2px((float) (dealDatas.get(i) * 10), getContext()) + y_SmallCell * spaceY
                        , 3 * spaceX + (i + 1) * len
                        , UiUtils.Xmm2px((float) (dealDatas.get(i + 1) * 10), getContext()) + y_SmallCell * spaceY
                        , DataPaint);
            }
            if ((int) Math.floor((i) / 1000) == 1) {
                canvas.drawLine(0 * spaceX + (i - 1 * 1000) * len
                        , UiUtils.Xmm2px((float) (dealDatas.get(i) * 10), getContext()) + y_SmallCell * (3) * spaceY
                        , 0 * spaceX + (i - 1 * 1000 + 1) * len
                        , UiUtils.Xmm2px((float) (dealDatas.get(i + 1) * 10), getContext()) + y_SmallCell * (3) * spaceY
                        , DataPaint);
            }
            if ((int) Math.floor((i) / 1000) == 2) {
                canvas.drawLine(0 * spaceX + (i - 2 * 1000) * len
                        , UiUtils.Xmm2px((float) (dealDatas.get(i) * 10), getContext()) + y_SmallCell * (5) * spaceY
                        , 0 * spaceX + (i - 2 * 1000 + 1) * len
                        , UiUtils.Xmm2px((float) (dealDatas.get(i + 1) * 10), getContext()) + y_SmallCell * (5) * spaceY
                        , DataPaint);
            }
            if ((int) Math.floor((i) / 1000) == 3) {
                canvas.drawLine(0 * spaceX + (i - 3 * 1000) * len
                        , UiUtils.Xmm2px((float) (dealDatas.get(i) * 10), getContext()) + y_SmallCell * (7) * spaceY
                        , 0 * spaceX + (i - 3 * 1000 + 1) * len
                        , UiUtils.Xmm2px((float) (dealDatas.get(i + 1) * 10), getContext()) + y_SmallCell * (7) * spaceY
                        , DataPaint);
            }
        }
    }

    public void DrawDataOtherPage(Canvas canvas) {
        int y_SmallCell = 6;
        for (int i = 0; i < dealDatas.size() - 1; i++) {
            if ((int) Math.floor((i) / 1000) == 0) {
                canvas.drawLine(0 * spaceX + i * len
                        , UiUtils.Xmm2px((float) (dealDatas.get(i) * 10), getContext()) + y_SmallCell * spaceY
                        , 0 * spaceX + (i + 1) * len
                        , UiUtils.Xmm2px((float) (dealDatas.get(i + 1) * 10), getContext()) + y_SmallCell * spaceY
                        , DataPaint);
            }
            if ((int) Math.floor((i) / 1000) == 1) {
                canvas.drawLine(0 * spaceX + (i - 1 * 1000) * len
                        , UiUtils.Xmm2px((float) (dealDatas.get(i) * 10), getContext()) + y_SmallCell * (3) * spaceY
                        , 0 * spaceX + (i - 1 * 1000 + 1) * len
                        , UiUtils.Xmm2px((float) (dealDatas.get(i + 1) * 10), getContext()) + y_SmallCell * (3) * spaceY
                        , DataPaint);
            }
            if ((int) Math.floor((i) / 1000) == 2) {
                canvas.drawLine(0 * spaceX + (i - 2 * 1000) * len
                        , UiUtils.Xmm2px((float) (dealDatas.get(i) * 10), getContext()) + y_SmallCell * (5) * spaceY
                        , 0 * spaceX + (i - 2 * 1000 + 1) * len
                        , UiUtils.Xmm2px((float) (dealDatas.get(i + 1) * 10), getContext()) + y_SmallCell * (5) * spaceY
                        , DataPaint);
            }
            if ((int) Math.floor((i) / 1000) == 3) {
                canvas.drawLine(0 * spaceX + (i - 3 * 1000) * len
                        , UiUtils.Xmm2px((float) (dealDatas.get(i) * 10), getContext()) + y_SmallCell * (7) * spaceY
                        , 0 * spaceX + (i - 3 * 1000 + 1) * len
                        , UiUtils.Xmm2px((float) (dealDatas.get(i + 1) * 10), getContext()) + y_SmallCell * (7) * spaceY
                        , DataPaint);
            }
        }
    }

    public List<Double> getDealDatas() {
        return dealDatas;
    }

    public void setDealDatas(List<Double> dealDatas) {
        this.dealDatas = dealDatas;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

}
